<?php

namespace SLN0002\Service;

use Home\Common\FIdConst;
use Home\DAO\OrgDAO;
use Home\Service\BizlogService;
use Home\Service\PinyinService;
use Home\Service\PSIBaseExService;
use SLN0002\DAO\SubjectDAO;

/**
 * 会计科目 Service
 *
 * @author PSI
 * @copyright 2015 - present
 * @license GPL v3
 */
class SubjectService extends PSIBaseExService
{
  private $LOG_CATEGORY = "会计科目";

  /**
   * 返回所有的公司列表
   *
   * @return array
   */
  public function companyList()
  {
    if ($this->isNotOnline()) {
      return $this->emptyResult();
    }

    $params = [
      "loginUserId" => $this->getLoginUserId(),
      "fid" => FIdConst::GL_SUBJECT
    ];

    $dao = new OrgDAO($this->db());
    return $dao->getCompanyExList($params);
  }

  /**
   * 某个公司的科目码列表
   *
   * @param array $params
   * @return array
   */
  public function subjectList($params)
  {
    if ($this->isNotOnline()) {
      return $this->emptyResult();
    }

    $dao = new SubjectDAO($this->db());
    return $dao->subjectList($params);
  }

  /**
   * 初始国家标准科目
   */
  public function initStandardSubject($params)
  {
    if ($this->isNotOnline()) {
      return $this->notOnlineError();
    }

    $params["dataOrg"] = $this->getLoginUserDataOrg();

    $db = $this->db();
    $db->startTrans();

    $dao = new SubjectDAO($db);
    $rc = $dao->initStandardSubject($params, new PinyinService());
    if ($rc) {
      $db->rollback();
      return $rc;
    }

    // 记录业务日志
    $companyName = $params["companyName"];
    $log = "为[{$companyName}]初始化国家标准会计科目";
    $bs = new BizlogService($db);
    $bs->insertBizlog($log, $this->LOG_CATEGORY);

    $db->commit();

    // 创建实际的总账数据库表
    // 因为MySQL中DDL是自动事务，所以需要写在上面的事务之外
    $companyId = $params["id"];
    $dao->createGlTableForStandardSubject($companyId);

    return $this->ok();
  }

  /**
   * 新增或编辑会计科目
   *
   * @param array $params
   * @return array
   */
  public function editSubject($params)
  {
    if ($this->isNotOnline()) {
      return $this->notOnlineError();
    }

    $id = $params["id"];

    $params["dataOrg"] = $this->getLoginUserDataOrg();

    $db = $this->db();
    $db->startTrans();

    $dao = new SubjectDAO($db);
    if ($id) {
      // 编辑
      $rc = $dao->updateSubject($params);
      if ($rc) {
        $db->rollback();
        return $rc;
      }
    } else {
      // 新增
      $rc = $dao->addSubject($params);
      if ($rc) {
        $db->rollback();
        return $rc;
      }
      $id = $params["id"];
    }

    // 记录业务日志
    $log = $params["log"];
    $bs = new BizlogService($db);
    $bs->insertBizlog($log, $this->LOG_CATEGORY);

    $db->commit();

    return $this->ok($id);
  }

  /**
   * 上级科目字段 - 查询数据
   *
   * @param string $queryKey
   */
  public function queryDataForParentSubject($queryKey, $companyId)
  {
    if ($this->isNotOnline()) {
      return $this->emptyResult();
    }

    $dao = new SubjectDAO($this->db());
    return $dao->queryDataForParentSubject($queryKey, $companyId);
  }

  /**
   * 某个科目的详情
   *
   * @param array $params
   */
  public function subjectInfo($params)
  {
    if ($this->isNotOnline()) {
      return $this->emptyResult();
    }

    $dao = new SubjectDAO($this->db());
    return $dao->subjectInfo($params);
  }

  /**
   * 删除科目
   *
   * @param array $params
   */
  public function deleteSubject($params)
  {
    if ($this->isNotOnline()) {
      return $this->notOnlineError();
    }

    $db = $this->db();

    $db->startTrans();
    $dao = new SubjectDAO($db);

    $rc = $dao->deleteSubject($params);
    if ($rc) {
      $db->rollback();
      return $rc;
    }

    // 记录业务日志
    $code = $params["code"];
    $log = "删除科目: $code";
    $bs = new BizlogService($db);
    $bs->insertBizlog($log, $this->LOG_CATEGORY);

    $db->commit();

    return $this->ok();
  }

  /**
   * 初始化科目的标准账样
   *
   * @param array $params
   */
  public function initFmt($params)
  {
    if ($this->isNotOnline()) {
      return $this->notOnlineError();
    }

    $db = $this->db();

    $db->startTrans();
    $dao = new SubjectDAO($db);

    $params["dataOrg"] = $this->getLoginUserDataOrg();
    $rc = $dao->initFmt($params);
    if ($rc) {
      $db->rollback();
      return $rc;
    }

    // 记录业务日志
    $log = $params["log"];
    $bs = new BizlogService($db);
    $bs->insertBizlog($log, $this->LOG_CATEGORY);

    $db->commit();

    // 新建账样，创建数据库中的实际物理表
    // 因为MySQL的DDL自动提交事务，所以该代码在创建元数据之后执行
    // 也不需要启动事务

    $rc = $dao->createTableForInitFmt($params);
    if ($rc) {
      return $rc;
    }

    return $this->ok();
  }

  /**
   * 某个科目的账样属性
   *
   * @param array $params
   */
  public function fmtPropList($params)
  {
    if ($this->isNotOnline()) {
      return $this->emptyResult();
    }

    $dao = new SubjectDAO($this->db());
    return $dao->fmtPropList($params);
  }

  /**
   * 某个科目的账样字段列表
   *
   * @param array $params
   */
  public function fmtColsList($params)
  {
    if ($this->isNotOnline()) {
      return $this->emptyResult();
    }

    $dao = new SubjectDAO($this->db());
    return $dao->fmtColsList($params);
  }

  /**
   * 新增或编辑账样字段
   */
  public function editFmtCol($params)
  {
    if ($this->isNotOnline()) {
      return $this->notOnlineError();
    }

    $id = $params["id"];

    if ($this->isDemo()) {
      $n = $id ? "编辑" : "新建";
      return $this->bad("演示环境下，不能{$n}账样列");
    }

    $db = $this->db();

    $db->startTrans();
    $dao = new SubjectDAO($db);

    $log = null;
    $isModify = false;
    if ($id) {
      // 编辑账样字段
      $isModify = true;
      $rc = $dao->updateFmtCol($params);
      if ($rc) {
        $db->rollback();
        return $rc;
      }
    } else {
      // 新增账样字段
      $rc = $dao->addFmtCol($params);
      if ($rc) {
        $db->rollback();
        return $rc;
      }

      $id = $params["id"];
    }

    // 记录业务日志
    $log = $params["log"];
    $bs = new BizlogService($db);
    $bs->insertBizlog($log, $this->LOG_CATEGORY);

    $db->commit();

    if (!$isModify) {
      // 新建账样列，同时创建数据库表字段
      $rc = $dao->createTableColForFMT($params);
      if ($rc) {
        return $rc;
      }
    }

    return $this->ok($id);
  }

  /**
   * 获得某个账样字段的详情
   */
  public function fmtColInfo($params)
  {
    if ($this->isNotOnline()) {
      return $this->emptyResult();
    }

    $dao = new SubjectDAO($this->db());
    return $dao->fmtColInfo($params);
  }

  /**
   * 删除某个账样字段
   */
  public function deleteFmtCol($params)
  {
    if ($this->isNotOnline()) {
      return $this->notOnlineError();
    }

    $db = $this->db();

    $db->startTrans();
    $dao = new SubjectDAO($db);

    $rc = $dao->deleteFmtCol($params);
    if ($rc) {
      $db->rollback();
      return $rc;
    }

    // 记录业务日志
    $caption = $params["caption"];
    $code = $params["subjectCode"];
    $accNumber = $params["accNumber"];
    $log = "删除科目[{$code}]账样[账样号 = {$accNumber}]的字段: $caption";
    $bs = new BizlogService($db);
    $bs->insertBizlog($log, $this->LOG_CATEGORY);

    $db->commit();

    return $this->ok();
  }

  /**
   * 某个账样所有字段 - 设置字段显示次序用
   */
  public function fmtGridColsList($params)
  {
    if ($this->isNotOnline()) {
      return $this->emptyResult();
    }

    $dao = new SubjectDAO($this->db());
    return $dao->fmtGridColsList($params);
  }

  /**
   * 编辑账样字段的显示次序
   */
  public function editFmtColShowOrder($params)
  {
    if ($this->isNotOnline()) {
      return $this->notOnlineError();
    }

    $db = $this->db();

    $db->startTrans();
    $dao = new SubjectDAO($db);

    $rc = $dao->editFmtColShowOrder($params);
    if ($rc) {
      $db->rollback();
      return $rc;
    }

    // 记录业务日志
    $code = $params["subjectCode"];
    $log = "编辑科目[{$code}]账样字段的显示次序";
    $bs = new BizlogService($db);
    $bs->insertBizlog($log, $this->LOG_CATEGORY);

    $db->commit();

    return $this->ok();
  }

  /**
   * 选择值来源的引用列 - 查询表
   */
  public function queryTablesForColRef($params)
  {
    if ($this->isNotOnline()) {
      return $this->emptyResult();
    }

    $dao = new SubjectDAO($this->db());
    return $dao->queryTablesForColRef($params);
  }

  /**
   * 科目自定义字段查询数据
   */
  public function queryDataForSubjectField($params)
  {
    if ($this->isNotOnline()) {
      return $this->emptyResult();
    }

    $dao = new SubjectDAO($this->db());
    return $dao->queryDataForSubjectField($params);
  }

  /**
   * 一键生成数量金额三栏账样列
   */
  public function genCPMFields($params)
  {
    if ($this->isNotOnline()) {
      return $this->notOnlineError();
    }

    $db = $this->db();

    $db->startTrans();
    $dao = new SubjectDAO($db);

    $rc = $dao->genCPMFields($params);
    if ($rc) {
      $db->rollback();
      return $rc;
    }

    // 记录业务日志
    $log = $params["log"];
    $bs = new BizlogService($db);
    $bs->insertBizlog($log, $this->LOG_CATEGORY);

    $db->commit();

    // 创建实际数据库表字段
    $rc = $dao->createDbFieldsForCPM($params);
    if ($rc) {
      return $rc;
    }

    return $this->ok();
  }

  /**
   * 初始化标准进销存账样
   */
  public function initStandardFmtForSLN0001($params)
  {
    if ($this->isNotOnline()) {
      return $this->notOnlineError();
    }

    $params["dataOrg"] = $this->getLoginUserDataOrg();
    $params["pinyinService"] = new PinyinService();

    $db = $this->db();

    $db->startTrans();
    $dao = new SubjectDAO($db);

    $rc = $dao->initStandardFmtForSLN0001($params);
    if ($rc) {
      $db->rollback();
      return $rc;
    }

    // 记录业务日志
    $log = $params["log"];
    $bs = new BizlogService($db);
    $bs->insertBizlog($log, $this->LOG_CATEGORY);

    $db->commit();

    // 创建数据库表
    // 因为MySQL中DDL是自动事务，所以需要写在上面的事务之外
    $companyId = $params["companyId"];
    $vatType = $params["vatType"];
    if (!in_array($vatType, [1, 2])) {
      return $this->bad("vatType参数错误");
    }

    // 创建总账表
    $dao->createGlTableForStandardSubject($companyId);

    // 科目1001 - 库存现金
    $id100101 = $params["id100101"];
    $rc = $this->createTable_SLN0001_1001($dao, $companyId, $id100101);
    if ($rc) {
      return $rc;
    }

    // 科目1002 - 银行存款
    $id100201 = $params["id100201"];
    $rc = $this->createTable_SLN0001_1002($dao, $companyId, $id100201);
    if ($rc) {
      return $rc;
    }

    // 科目1122 - 应收账款
    $id112201 = $params["id112201"];
    $id112202 = $params["id112202"];
    $rc = $this->createTable_SLN0001_1122($dao, $companyId, $id112201, $id112202);
    if ($rc) {
      return $rc;
    }

    // 科目1123 - 预付账款
    $id112301 = $params["id112301"];
    $rc = $this->createTable_SLN0001_1123($dao, $companyId, $id112301);
    if ($rc) {
      return $rc;
    }

    // 科目1402 - 在途物资
    $id140201 = $params["id140201"];
    $rc = $this->createTable_SLN0001_1402($dao, $companyId, $id140201);
    if ($rc) {
      return $rc;
    }

    // 科目1405 - 库存商品
    $id140501 = $params["id140501"];
    $rc = $this->createTable_SLN0001_1405($dao, $companyId, $id140501);
    if ($rc) {
      return $rc;
    }

    // 科目2202 - 应付账款
    $id220201 = $params["id220201"];
    $id220202 = $params["id220202"];
    $rc = $this->createTable_SLN0001_2202($dao, $companyId, $id220201, $id220202);
    if ($rc) {
      return $rc;
    }

    // 科目2221 - 应交税费
    $rc = null;
    if ($vatType == 1) {
      $id22210101 = $params["id22210101"];
      $id22210102 = $params["id22210102"];
      $id222102 = $params["id222102"];
      $rc = $this->createTable_SLN0001_2221_general($dao, $companyId, $id22210101, $id22210102, $id222102);
    } else {
      $id222101 = $params["id222101"];
      $rc = $this->createTable_SLN0001_2221_samll($dao, $companyId, $id222101);
    }
    if ($rc) {
      return $rc;
    }

    // 科目4103 - 本年利润
    $id4103 = $params["id4103"];
    $rc = $this->createTable_SLN0001_4103($dao, $companyId, $id4103);
    if ($rc) {
      return $rc;
    }

    // 科目6001 - 主营业务收入
    $id600101 = $params["id600101"];
    $rc = $this->createTable_SLN0001_6001($dao, $companyId, $id600101);
    if ($rc) {
      return $rc;
    }

    // 科目6401 - 主营业务成本
    $id640101 = $params["id640101"];
    $rc = $this->createTable_SLN0001_6401($dao, $companyId, $id640101);
    if ($rc) {
      return $rc;
    }

    // 科目6601 - 销售费用
    $id660101 = $params["id660101"];
    $id660102 = $params["id660102"];
    $rc = $this->createTable_SLN0001_6601($dao, $companyId, $id660101, $id660102);
    if ($rc) {
      return $rc;
    }

    // 科目6602 - 管理费用
    $id660201 = $params["id660201"];
    $id660202 = $params["id660202"];
    $rc = $this->createTable_SLN0001_6602($dao, $companyId, $id660201, $id660202);
    if ($rc) {
      return $rc;
    }

    // 科目6603 - 财务费用
    $id660301 = $params["id660301"];
    $id660302 = $params["id660302"];
    $rc = $this->createTable_SLN0001_6603($dao, $companyId, $id660301, $id660302);
    if ($rc) {
      return $rc;
    }

    // 操作成功
    return $this->ok();
  }

  /**
   * 标准进销存 - 创建，科目1001 - 库存现金，相关的数据库表
   */
  private function createTable_SLN0001_1001($dao, $companyId, $id100101)
  {
    $createParams = [
      "companyId" => $companyId,
      "id" => $id100101,
    ];
    $rc = $dao->createTableForInitFmt($createParams);
    if ($rc) {
      return $rc;
    }

    // 操作成功
    return null;
  }

  /**
   * 标准进销存 - 创建，科目1002 - 银行存款，相关的数据库表
   */
  private function createTable_SLN0001_1002($dao, $companyId, $id100201)
  {
    $createParams = [
      "companyId" => $companyId,
      "id" => $id100201,
    ];
    $rc = $dao->createTableForInitFmt($createParams);
    if ($rc) {
      return $rc;
    }

    // 科目100201的银行账号字段
    $createParams = [
      "companyId" => $companyId,
      "subjectCode" => "100201",
      "fieldName" => "x_bank",
      "fieldType" => 1,
      "voucherInput" => 2,
    ];
    $rc = $dao->createTableColForFMT($createParams);
    if ($rc) {
      return $rc;
    }

    // 操作成功
    return null;
  }

  /**
   * 标准进销存 - 创建，科目1122 - 应收账款，相关的数据库表
   */
  private function createTable_SLN0001_1122($dao, $companyId, $id112201, $id112202)
  {
    // 112201
    $createParams = [
      "companyId" => $companyId,
      "id" => $id112201,
    ];
    $rc = $dao->createTableForInitFmt($createParams);
    if ($rc) {
      return $rc;
    }

    // 科目112201的客户字段
    $createParams = [
      "companyId" => $companyId,
      "subjectCode" => "112201",
      "fieldName" => "x_customer",
      "fieldType" => 1,
      "voucherInput" => 2,
    ];
    $rc = $dao->createTableColForFMT($createParams);
    if ($rc) {
      return $rc;
    }

    // 112202
    $createParams = [
      "companyId" => $companyId,
      "id" => $id112202,
    ];
    $rc = $dao->createTableForInitFmt($createParams);
    if ($rc) {
      return $rc;
    }

    // 科目112202的客户字段
    $createParams = [
      "companyId" => $companyId,
      "subjectCode" => "112202",
      "fieldName" => "x_customer",
      "fieldType" => 1,
      "voucherInput" => 2,
    ];
    $rc = $dao->createTableColForFMT($createParams);
    if ($rc) {
      return $rc;
    }

    // 操作成功
    return null;
  }

  /**
   * 标准进销存 - 创建，科目1123 - 预付账款，相关的数据库表
   */
  private function createTable_SLN0001_1123($dao, $companyId, $id112301)
  {
    // 科目112301
    $createParams = [
      "companyId" => $companyId,
      "id" => $id112301,
    ];
    $rc = $dao->createTableForInitFmt($createParams);
    if ($rc) {
      return $rc;
    }

    // 科目112301的供应商字段
    $createParams = [
      "companyId" => $companyId,
      "subjectCode" => "112301",
      "fieldName" => "x_supplier",
      "fieldType" => 1,
      "voucherInput" => 2,
    ];
    $rc = $dao->createTableColForFMT($createParams);
    if ($rc) {
      return $rc;
    }

    // 操作成功
    return null;
  }

  /**
   * 标准进销存 - 创建，科目1402 - 在途物资，相关的数据库表
   */
  private function createTable_SLN0001_1402($dao, $companyId, $id140201)
  {
    $createParams = [
      "companyId" => $companyId,
      "id" => $id140201,
    ];
    $rc = $dao->createTableForInitFmt($createParams);
    if ($rc) {
      return $rc;
    }

    // 科目140201的物料字段
    $createParams = [
      "companyId" => $companyId,
      "subjectCode" => "140201",
      "fieldName" => "x_goods",
      "fieldType" => 1,
      "voucherInput" => 2,
    ];
    $rc = $dao->createTableColForFMT($createParams);
    if ($rc) {
      return $rc;
    }

    // 科目1402的数量金额三栏字段
    $createParams = [
      "companyId" => $companyId,
      "subjectCode" => "140201",
    ];
    $rc = $dao->createDbFieldsForCPM($createParams);
    if ($rc) {
      return $rc;
    }

    // 操作成功
    return null;
  }

  /**
   * 标准进销存 - 创建，科目1405 - 库存商品，相关的数据库表
   */
  private function createTable_SLN0001_1405($dao, $companyId, $id140501)
  {
    $createParams = [
      "companyId" => $companyId,
      "id" => $id140501,
    ];
    $rc = $dao->createTableForInitFmt($createParams);
    if ($rc) {
      return $rc;
    }

    // 科目140501的仓库字段
    $createParams = [
      "companyId" => $companyId,
      "subjectCode" => "140501",
      "fieldName" => "x_warehouse",
      "fieldType" => 1,
      "voucherInput" => 2,
    ];
    $rc = $dao->createTableColForFMT($createParams);
    if ($rc) {
      return $rc;
    }

    // 科目140501的物料字段
    $createParams = [
      "companyId" => $companyId,
      "subjectCode" => "140501",
      "fieldName" => "x_goods",
      "fieldType" => 1,
      "voucherInput" => 2,
    ];
    $rc = $dao->createTableColForFMT($createParams);
    if ($rc) {
      return $rc;
    }

    // 科目140501的数量金额三栏字段
    $createParams = [
      "companyId" => $companyId,
      "subjectCode" => "140501",
    ];
    $rc = $dao->createDbFieldsForCPM($createParams);
    if ($rc) {
      return $rc;
    }

    // 操作成功
    return null;
  }

  /**
   * 标准进销存 - 创建，科目2202 - 应付账款，相关的数据库表
   */
  private function createTable_SLN0001_2202($dao, $companyId, $id220201, $id220202)
  {
    // 科目220201
    $createParams = [
      "companyId" => $companyId,
      "id" => $id220201,
    ];
    $rc = $dao->createTableForInitFmt($createParams);
    if ($rc) {
      return $rc;
    }

    // 科目220201的供应商字段
    $createParams = [
      "companyId" => $companyId,
      "subjectCode" => "220201",
      "fieldName" => "x_supplier",
      "fieldType" => 1,
      "voucherInput" => 2,
    ];
    $rc = $dao->createTableColForFMT($createParams);
    if ($rc) {
      return $rc;
    }

    // 科目220202
    $createParams = [
      "companyId" => $companyId,
      "id" => $id220202,
    ];
    $rc = $dao->createTableForInitFmt($createParams);
    if ($rc) {
      return $rc;
    }

    // 科目220202的供应商字段
    $createParams = [
      "companyId" => $companyId,
      "subjectCode" => "220202",
      "fieldName" => "x_supplier",
      "fieldType" => 1,
      "voucherInput" => 2,
    ];
    $rc = $dao->createTableColForFMT($createParams);
    if ($rc) {
      return $rc;
    }

    // 操作成功
    return null;
  }

  /**
   * 标准进销存 - 创建，科目4103 - 本年利润，相关的数据库表
   */
  private function createTable_SLN0001_4103($dao, $companyId, $id4103)
  {
    $createParams = [
      "companyId" => $companyId,
      "id" => $id4103,
    ];
    $rc = $dao->createTableForInitFmt($createParams);
    if ($rc) {
      return $rc;
    }

    // 操作成功
    return null;
  }

  /**
   * 标准进销存 - 创建，科目2221 - 应交税费，相关的数据库表，小规模纳税人使用
   */
  private function createTable_SLN0001_2221_samll($dao, $companyId, $id222101)
  {
    // 科目222101
    $createParams = [
      "companyId" => $companyId,
      "id" => $id222101,
    ];
    $rc = $dao->createTableForInitFmt($createParams);
    if ($rc) {
      return $rc;
    }

    // 操作成功
    return null;
  }

  /**
   * 标准进销存 - 创建，科目2221 - 应交税费，相关的数据库表，一般纳税人使用
   */
  private function createTable_SLN0001_2221_general($dao, $companyId, $id22210101, $id22210102, $id222102)
  {

    // 科目22210101
    $createParams = [
      "companyId" => $companyId,
      "id" => $id22210101,
    ];
    $rc = $dao->createTableForInitFmt($createParams);
    if ($rc) {
      return $rc;
    }

    // 科目22210102
    $createParams = [
      "companyId" => $companyId,
      "id" => $id22210102,
    ];
    $rc = $dao->createTableForInitFmt($createParams);
    if ($rc) {
      return $rc;
    }

    // 科目222102
    $createParams = [
      "companyId" => $companyId,
      "id" => $id222102,
    ];
    $rc = $dao->createTableForInitFmt($createParams);
    if ($rc) {
      return $rc;
    }

    // 操作成功
    return null;
  }

  /**
   * 标准进销存 - 创建，科目6001 - 主营业务收入，相关的数据库表
   */
  private function createTable_SLN0001_6001($dao, $companyId, $id600101)
  {
    // 科目600101
    $createParams = [
      "companyId" => $companyId,
      "id" => $id600101,
    ];
    $rc = $dao->createTableForInitFmt($createParams);
    if ($rc) {
      return $rc;
    }

    // 科目600101的客户字段
    $createParams = [
      "companyId" => $companyId,
      "subjectCode" => "600101",
      "fieldName" => "x_customer",
      "fieldType" => 1,
      "voucherInput" => 2,
    ];
    $rc = $dao->createTableColForFMT($createParams);
    if ($rc) {
      return $rc;
    }

    // 操作成功
    return null;
  }

  /**
   * 标准进销存 - 创建，科目6401 - 主营业务收入，相关的数据库表
   */
  private function createTable_SLN0001_6401($dao, $companyId, $id640101)
  {
    $createParams = [
      "companyId" => $companyId,
      "id" => $id640101,
    ];
    $rc = $dao->createTableForInitFmt($createParams);
    if ($rc) {
      return $rc;
    }

    // 科目640101的仓库字段
    $createParams = [
      "companyId" => $companyId,
      "subjectCode" => "640101",
      "fieldName" => "x_warehouse",
      "fieldType" => 1,
      "voucherInput" => 2,
    ];
    $rc = $dao->createTableColForFMT($createParams);
    if ($rc) {
      return $rc;
    }

    // 科目640101的物料字段
    $createParams = [
      "companyId" => $companyId,
      "subjectCode" => "640101",
      "fieldName" => "x_goods",
      "fieldType" => 1,
      "voucherInput" => 2,
    ];
    $rc = $dao->createTableColForFMT($createParams);
    if ($rc) {
      return $rc;
    }

    // 操作成功
    return null;
  }

  /**
   * 标准进销存 - 创建，科目6601 - 销售费用，相关的数据库表
   */
  private function createTable_SLN0001_6601($dao, $companyId, $id660101, $id660102)
  {
    // 660101
    $createParams = [
      "companyId" => $companyId,
      "id" => $id660101,
    ];
    $rc = $dao->createTableForInitFmt($createParams);
    if ($rc) {
      return $rc;
    }

    // 科目660101的费用项字段
    $createParams = [
      "companyId" => $companyId,
      "subjectCode" => "660101",
      "fieldName" => "x_expense",
      "fieldType" => 1,
      "voucherInput" => 2,
    ];
    $rc = $dao->createTableColForFMT($createParams);
    if ($rc) {
      return $rc;
    }

    // 660102
    $createParams = [
      "companyId" => $companyId,
      "id" => $id660102,
    ];
    $rc = $dao->createTableForInitFmt($createParams);
    if ($rc) {
      return $rc;
    }

    // 科目660102的费用项字段
    $createParams = [
      "companyId" => $companyId,
      "subjectCode" => "660102",
      "fieldName" => "x_expense",
      "fieldType" => 1,
      "voucherInput" => 2,
    ];
    $rc = $dao->createTableColForFMT($createParams);
    if ($rc) {
      return $rc;
    }

    // 操作成功
    return null;
  }

  /**
   * 标准进销存 - 创建，科目6602 - 管理费用，相关的数据库表
   */
  private function createTable_SLN0001_6602($dao, $companyId, $id660201, $id660202)
  {
    // 660201
    $createParams = [
      "companyId" => $companyId,
      "id" => $id660201,
    ];
    $rc = $dao->createTableForInitFmt($createParams);
    if ($rc) {
      return $rc;
    }

    // 科目660201的费用项字段
    $createParams = [
      "companyId" => $companyId,
      "subjectCode" => "660201",
      "fieldName" => "x_expense",
      "fieldType" => 1,
      "voucherInput" => 2,
    ];
    $rc = $dao->createTableColForFMT($createParams);
    if ($rc) {
      return $rc;
    }

    // 660202
    $createParams = [
      "companyId" => $companyId,
      "id" => $id660202,
    ];
    $rc = $dao->createTableForInitFmt($createParams);
    if ($rc) {
      return $rc;
    }

    // 科目660202的费用项字段
    $createParams = [
      "companyId" => $companyId,
      "subjectCode" => "660202",
      "fieldName" => "x_expense",
      "fieldType" => 1,
      "voucherInput" => 2,
    ];
    $rc = $dao->createTableColForFMT($createParams);
    if ($rc) {
      return $rc;
    }

    // 操作成功
    return null;
  }

  /**
   * 标准进销存 - 创建，科目6603 - 财务费用，相关的数据库表
   */
  private function createTable_SLN0001_6603($dao, $companyId, $id660301, $id660302)
  {
    // 660301
    $createParams = [
      "companyId" => $companyId,
      "id" => $id660301,
    ];
    $rc = $dao->createTableForInitFmt($createParams);
    if ($rc) {
      return $rc;
    }

    // 科目660301的费用项目字段
    $createParams = [
      "companyId" => $companyId,
      "subjectCode" => "660301",
      "fieldName" => "x_expense",
      "fieldType" => 1,
      "voucherInput" => 2,
    ];
    $rc = $dao->createTableColForFMT($createParams);
    if ($rc) {
      return $rc;
    }

    // 660302
    $createParams = [
      "companyId" => $companyId,
      "id" => $id660302,
    ];
    $rc = $dao->createTableForInitFmt($createParams);
    if ($rc) {
      return $rc;
    }

    // 科目660302的费用项目字段
    $createParams = [
      "companyId" => $companyId,
      "subjectCode" => "660302",
      "fieldName" => "x_expense",
      "fieldType" => 1,
      "voucherInput" => 2,
    ];
    $rc = $dao->createTableColForFMT($createParams);
    if ($rc) {
      return $rc;
    }

    // 操作成功
    return null;
  }
}
